<?php

include('../../include/functions.inc.php');
if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 'cargos':
            listCargos();
            break;
        case 'stud':
            listStudents();
            break;
        case 'save':
            save($db, $user);
            break;
    }
}

function listStudents() {
    header('Location: ../students/slistStudents.php');
}

function listCargos() {
    header('Location: ../cargos/list.php');
}

function save($db, $user) {
    $student = getParam('ID');
    $cy = getCycle($db, $user);
    $rs = $db->query_select('cllg_account', 'FK_STUDENT=' . $student);
    if (count($rs) == 0) {//registra una nueva cuenta
        $db->query_insert('cllg_account', array(
            'FK_STUDENT' => sqlValue($student, 'int'),
            'STATUS' => 1,
            'CURR_BALANCE' => sqlValue("0", 'double'),
            'PREV_BALANCE' => sqlValue("0", 'double'),
            'FK_ENTITY_OWNER' => sqlValue($user->entity, 'int'),
            'CREATED_BY' => sqlValue($user->name, 'text'),
            'CREATED' => sqlValue(date("y-m-d"), 'date')));
        $rs = $db->query_select('cllg_account', 'FK_STUDENT=' . $student);
    }
    $ac = $rs[0];
    $cBalance = $ac['CURR_BALANCE'];
    $cargos = json_decode(stripslashes(getParam('json')), TRUE);
    $date = date('y-m-d');
    $assign = $db->query_select("cllg_student_assign", "FK_STUDENT={$student} AND STATUS=1 AND CREATED IN (
                                                            SELECT MAX(CREATED) 
                                                            FROM cllg_student_assign
                                                            WHERE FK_STUDENT={$student}
                                                            AND STATUS=1)");
    $assignData = count($assign) == 0 ? "" : $assign[0]['ID']; 
    foreach ($cargos as $cargo) {
        $pBalance = $cBalance;
        $cBalance+=$cargo['MONTO'];
        $db->query_insert("cllg_cargo", array(
            "FK_ACCOUNT" => sqlValue($ac['ID'], 'int'),
            "DATE_LIMIT" => sqlValue($cargo["FECHA_PAGO"], 'date'),
            "PENALTY" => sqlValue($cargo['MORA'], 'float'),
            "DESCRIPTION" => sqlValue($cargo['NOMBRE'], 'text'),
            "CREATED" => sqlValue($date, 'date'),
            "CREATED_BY" => sqlValue($user->name, 'text'),
            "AMOUNT" => sqlValue($cargo['MONTO'], 'float'),
            "PREV_BALANCE" => sqlValue($pBalance, 'float'),
            "FK_STUDENT_ASSIGN" => sqlValue($assignData, 'int'),
            "CURR_BALANCE" => sqlValue($cBalance, 'float'),
            "MONEDA" => $cargo['MONEDA']));
    }
    $db->query_update('cllg_account', array('CURR_BALANCE' => $cBalance,
        'PREV_BALANCE' => $ac['CURR_BALANCE']), 'ID=' . $ac['ID']);
    $datos = array("NO_CARGOS" => count($cargos), "TOTAL" => $cBalance);
    $_SESSION['pack'] = serialize($datos);
    header('Location: ../../include/transport.php');
}

?>